package com.shenma2009.ex;

import org.apache.tomcat.util.log.SystemLogHandler;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author 军哥
 * @version 1.0
 * @description: MyExceptionHandler
 * @date 2023/6/2 15:23
 */

@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public String exceptionHandler(Exception e){
        System.out.println("全局异常捕获>>>:"+e);

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String msg = sw.toString();
        System.out.println(msg);

        // 把异常信息存入数据库
        //

        return "全局异常捕获,错误原因>>>"+msg;
    }
}

